export function PageMethods(API) {
    return {
        getPage() {
            this.loading++;
            API.fetchPage({})
                .then(data => {
                    this.loading--;
                    this.page = Object.assign(this.page, data.obj);
                })
                .catch(error => {
                    this.loading--;
                    console.error(error);
                });
        },
        // 编辑
        handleEdit: function(index, row) {
            this.openEdit(row.id);
        },
        // 添加
        handleAdd: function() {
            this.openEdit('');
        },
        // 删除
        handleDelete: function(index, row) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
            }).then(() => {
                this.loading++;
                API.deleteEntity(row.id)
                    .then(() => {
                        this.loading--;
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        this.getPage();
                    })
                    .catch(() => {
                        this.loading--;
                    });
            });
        },
        handleSizeChange: function(val) {
            this.page.size = val;
            this.getPage();
        },
        handleCurrentChange: function(val) {
            this.page.current = val;
            this.getPage();
        },
        handleContent(index, row) {
            // dictionary.TagType:  { 0: '知识点', 1: '题库' },
            if (row.type === 1) {
                this.$router.push({ path: '/problem/storage-manager-page', query: { tagId: row.id }});
            }
        },
        openEdit: function(id) {
            this.form.visible = true;
            setTimeout(() => {
                this.$refs['form'].init(id);
            }, 10);
        },
        handleSaved() {
            this.form.visible = false;
            this.getPage();
        }
    };
}

export function EditMethods(API) {
    return {
        init(id) {
            if (id) {
                this.getEntity(id);
            } else {
                this.entity = Object.assign({}, this.newEntity());
            }
        },
        newEntity() {
            return {
                id: null,
                name: '',
                sort: 0
            };
        },
        getEntity(id) {
            this.loading++;
            API.getEntity(id)
                .then(data => {
                    this.loading--;
                    this.entity = Object.assign(this.entity, data.obj);
                }).catch(() => {
                    this.loading--;
                });
        },
        handleSave(callback) {
            this.$refs['form'].validate((valid) => {
                if (!valid) {
                    this.$message({
                        type: 'warning',
                        message: '格式有误!'
                    });
                    return;
                }
                this.loading++;
                API.postEntity(this.entity)
                    .then(data => {
                        this.loading--;
                        this.entity = Object.assign(this.entity, data.obj);
                        this.$message({
                            type: ' success',
                            message: '保存成功'
                        });
                        callback && callback();
                        this.$emit('saved');
                    })
                    .catch(() => {
                        this.loading--;
                    });
            });
        },
        handleRelease() {
            this.entity.status = 1;
            this.handleSave();
        }
    };
}
